﻿using System.Windows.Input;
#if WINDOWS_PHONE
using System.Windows;
#elif NETFX_CORE
using Windows.UI.Xaml;

#endif
#if WINDOWS_PHONE || NETFX_CORE
namespace Nx.Commands
{
    public static class Hold
    {
        private static readonly DependencyProperty HoldCommandBehaviorProperty = DependencyProperty.RegisterAttached(
            "HoldCommandBehavior",
            typeof(ControlHoldCommandBehavior),
            typeof(Hold),
            null);

        public static readonly DependencyProperty CommandProperty = DependencyProperty.RegisterAttached(
            "Command",
            typeof(ICommand),
            typeof(Hold),
#if WINDOWS_PHONE
            new PropertyMetadata(OnSetCommandCallback));
#elif NETFX_CORE
 new PropertyMetadata(null, OnSetCommandCallback));
#endif

        public static readonly DependencyProperty CommandParameterProperty = DependencyProperty.RegisterAttached(
            "CommandParameter",
            typeof(object),
            typeof(Hold),
#if WINDOWS_PHONE
            new PropertyMetadata(OnSetCommandParameterCallback));
#elif NETFX_CORE
 new PropertyMetadata(null, OnSetCommandParameterCallback));
#endif

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters", Justification = "Only works for buttonbase")]
        public static void SetCommand(UIElement control, ICommand command)
        {
            if (control == null) throw new System.ArgumentNullException("control");
            control.SetValue(CommandProperty, command);
        }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters", Justification = "Only works for buttonbase")]
        public static ICommand GetCommand(UIElement control)
        {
            if (control == null) throw new System.ArgumentNullException("control");
            return control.GetValue(CommandProperty) as ICommand;
        }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters", Justification = "Only works for buttonbase")]
        public static void SetCommandParameter(UIElement control, object parameter)
        {
            if (control == null) throw new System.ArgumentNullException("buttonBase");
            control.SetValue(CommandParameterProperty, parameter);
        }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters", Justification = "Only works for buttonbase")]
        public static object GetCommandParameter(UIElement control)
        {
            if (control == null) throw new System.ArgumentNullException("buttonBase");
            return control.GetValue(CommandParameterProperty);
        }

        private static void OnSetCommandCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
        {
            UIElement control = dependencyObject as UIElement;
            if (control != null)
            {
                ControlHoldCommandBehavior behavior = GetOrCreateBehavior(control);
                behavior.Command = e.NewValue as ICommand;
            }
        }

        private static void OnSetCommandParameterCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
        {
            UIElement control = dependencyObject as UIElement;
            if (control != null)
            {
                ControlHoldCommandBehavior behavior = GetOrCreateBehavior(control);
                behavior.CommandParameter = e.NewValue;
            }
        }

        private static ControlHoldCommandBehavior GetOrCreateBehavior(UIElement control)
        {
            ControlHoldCommandBehavior behavior = control.GetValue(HoldCommandBehaviorProperty) as ControlHoldCommandBehavior;
            if (behavior == null)
            {
                behavior = new ControlHoldCommandBehavior(control);
                control.SetValue(HoldCommandBehaviorProperty, behavior);
            }

            return behavior;
        }
    }
}
#endif